package pl.edu.agh.mguesser.game.stats;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import pl.edu.agh.mguesser.game.listener.ScoreChangedListener;
import pl.edu.agh.mguesser.game.listener.ScoreChangedSource;

/**
 * @author keiran
 */
public class ScoreContainer implements ScoreChangedSource {
    
    private Map<String, Long> scores = new HashMap<>();
    private ScoreChangedListener scoreChangedListener;
    
    public void addScore(String user, long value) {
        Long score = scores.get(user);
        
        if(score == null) {
            score = 0l;
            scores.put(user, score);
        }
        
        scores.put(user, score + value);
        
        scoreChanged();
    }
    
    public Long getScore(String user) {
        return scores.get(user);
    }

    public Map<String, Long> getScores() {
        return Collections.unmodifiableMap(scores);
    }

    @Override
    public void setScoreChangedListener(ScoreChangedListener scoreChangedListener) {
        this.scoreChangedListener = scoreChangedListener;
    }

    ScoreChangedListener getScoreChangedListener() {
        return scoreChangedListener;
    }
    
    private void scoreChanged() {
        if(scoreChangedListener != null) {
            scoreChangedListener.scoreChanged(getScores());
        }
    }
    
}
